<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content>
    <meta name="keyword" content>
    <link rel="shortcut icon" href="/myblog/img/favicon.ico">

    <title>
        
          class文件结构 - Ethan的博客 | Ethan&#39;s Blog
        
    </title>

    <link rel="canonical" href="http://reflectyi.gitee.io/myblog/myblog/2016/10/01/jvm/jvm执行/class文件结构/">

    <!-- Bootstrap Core CSS -->
    <link rel="stylesheet" href="/myblog/css/bootstrap.min.css">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/myblog/css/hux-blog.min.css">

    <!-- Pygments Highlight CSS -->
    <link rel="stylesheet" href="/myblog/css/highlight.css">

    <!-- Custom Fonts -->
    <!-- <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"> -->
    <!-- Hux change font-awesome CDN to qiniu -->
    <link href="https://cdn.staticfile.org/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">


    <!-- Hux Delete, sad but pending in China
    <link href='http://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/
    css'>
    -->


    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- ga & ba script hoook -->
    <script></script>
</head>


<!-- hack iOS CSS :active style -->
<body ontouchstart="">

    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/myblog/">Hi,Ethan</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <!-- Known Issue, found by Hux:
            <nav>'s height woule be hold on by its content.
            so, when navbar scale out, the <nav> will cover tags.
            also mask any touch event of tags, unfortunately.
        -->
        <div id="huxblog_navbar">
            <div class="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/myblog/">首页</a>
                    </li>

                    

                        
                    

                        
                        <li>
                            <a href="/myblog/about/">目录</a>
                        </li>
                        
                    

                        
                        <li>
                            <a href="/myblog/archives/">时间轴</a>
                        </li>
                        
                    

                        
                        <li>
                            <a href="/myblog/tags/">标签</a>
                        </li>
                        
                    
                    
                </ul>
            </div>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>
<script>
    // Drop Bootstarp low-performance Navbar
    // Use customize navbar with high-quality material design animation
    // in high-perf jank-free CSS3 implementation
    var $body   = document.body;
    var $toggle = document.querySelector('.navbar-toggle');
    var $navbar = document.querySelector('#huxblog_navbar');
    var $collapse = document.querySelector('.navbar-collapse');

    $toggle.addEventListener('click', handleMagic)
    function handleMagic(e){
        if ($navbar.className.indexOf('in') > 0) {
        // CLOSE
            $navbar.className = " ";
            // wait until animation end.
            setTimeout(function(){
                // prevent frequently toggle
                if($navbar.className.indexOf('in') < 0) {
                    $collapse.style.height = "0px"
                }
            },400)
        }else{
        // OPEN
            $collapse.style.height = "auto"
            $navbar.className += " in";
        }
    }
</script>


    <!-- Main Content -->
    
<!-- Image to hack wechat -->
<!-- <img src="http://reflectyi.gitee.io/myblog/myblog/img/icon_wechat.png" width="0" height="0"> -->
<!-- <img src="{{ site.baseurl }}/{% if page.header-img %}{{ page.header-img }}{% else %}{{ site.header-img }}{% endif %}" width="0" height="0"> -->

<!-- Post Header -->
<style type="text/css">
    header.intro-header{
        background-image: url('/myblog/img/home-bg.jpg')
    }
</style>
<header class="intro-header" >
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <div class="tags">
                        
                          <a class="tag" href="/myblog/tags/#jvm" title="jvm">jvm</a>
                        
                          <a class="tag" href="/myblog/tags/#class" title="class">class</a>
                        
                    </div>
                    <h1>class文件结构</h1>
                    <h2 class="subheading">字节码文件组成及其作用</h2>
                    <span class="meta">
                        Posted by Ethan on
                        2016-10-01
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

    <!-- Post Container -->
            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                post-container">

                <h2 id="class-文件结构"><a href="#class-文件结构" class="headerlink" title="class 文件结构"></a>class 文件结构</h2><ul>
<li>class文件结构，自己感觉最晦涩难懂的部分。主要是理解class文件编译成字节码以后的存储方法</li>
<li>本次讨论内容主要是class文件，不包括动态生成。</li>
<li>class文件整体上可以划分为Code和元数据，两部分<h3 id="概述："><a href="#概述：" class="headerlink" title="概述："></a>概述：</h3></li>
<li>Class文件是以8位字节为基础单位的二进制流，jvm规范规定，class文件结构类似于c语言的伪结构体(包含两种数据结构)<ul>
<li>无符号数：基本数据类型。U1,U2,U4,U8分别代表1个字节，2个字节，4个字节和8个字节。<ul>
<li>描述数字，索引引用，数值量，或者按照UTF-8编码构成字符串值。</li>
</ul>
</li>
<li>表：由无符号数或者其他表作为数据项，组成的符合数据结构。<ul>
<li>描述有复合结构的数据。</li>
<li>整个Class文件就是一张表。 <h3 id="一、魔数"><a href="#一、魔数" class="headerlink" title="一、魔数"></a>一、魔数</h3></li>
</ul>
</li>
</ul>
</li>
<li>class文件起始4个字节称为魔数</li>
<li>不使用文件后缀区分是否是class文件，主要是基于安全考虑 </li>
<li>tip：类加载过程中，一边加载一边进行验证，他们是同步进行的</li>
<li>魔数值：0XCAFEBABE<h3 id="二、版本号"><a href="#二、版本号" class="headerlink" title="二、版本号"></a>二、版本号</h3></li>
<li>紧接着4个字节(5-8)，(5-6)class文件次版本号,(7-8)文件主版本号</li>
<li>jvm拒绝执行超过自己版本号的class文件<h3 id="三、版本号之后，常量池入口-常量池大小不固定"><a href="#三、版本号之后，常量池入口-常量池大小不固定" class="headerlink" title="三、版本号之后，常量池入口(常量池大小不固定)"></a>三、版本号之后，常量池入口(常量池大小不固定)</h3></li>
<li>1、常量池：<ul>
<li>class文件的资源仓库 </li>
<li>class文件中最大的数据项目之一，也是和其他项目关联最多的数据类型 </li>
<li>入口放置一个U2类型数据， 代表常量池计数容量(从1开始，在0时表示不引用常量池任何一个值)</li>
</ul>
</li>
<li>2、常量池中存放两大类常量：<ul>
<li>字面量 <ul>
<li>接近java的常量概念，文本字符串，生命final的常量值(JDK1.8以后把String移到了堆中)</li>
</ul>
</li>
<li>符号引用<ul>
<li>类和接口的全限定名(包名+类名) </li>
<li>字段的名称和描述符</li>
<li>方法的名称和描述符</li>
</ul>
</li>
<li>class文件中没有办法保存方法、字段的内存信息(Class只是一个文件)<ul>
<li>运行期间，常量池中的链接才能对应内存中的地址</li>
<li>常量池中每一项都是一个表<h3 id="四、访问标志"><a href="#四、访问标志" class="headerlink" title="四、访问标志"></a>四、访问标志</h3></li>
</ul>
</li>
</ul>
</li>
<li>常量池结束后，接着2个字节表示访问标志(类修饰符，类相关信息)<ul>
<li>类还是接口</li>
<li>是否是public</li>
<li>是否abstract</li>
<li>如果是类的话，是否是final等等<h3 id="五、类索引集合"><a href="#五、类索引集合" class="headerlink" title="五、类索引集合"></a>五、类索引集合</h3></li>
</ul>
</li>
<li>确定类的继承关系<ul>
<li>类索引和父类索引，都是U2类型<ul>
<li>类索引，确定这个类的全限定名</li>
<li>父类索引，确定该类的父类全限定名</li>
<li>除了Object外，所有类都有父类 </li>
</ul>
</li>
<li>接口是一组U2类型的索引数据集合<h3 id="六、字段表集合"><a href="#六、字段表集合" class="headerlink" title="六、字段表集合"></a>六、字段表集合</h3></li>
</ul>
</li>
<li>字段表描述接口或类中的变量(对象的成员变量)</li>
<li>包括class级变量和obj级别变量。但不包括方法内的变量<h3 id="七、方法表集合"><a href="#七、方法表集合" class="headerlink" title="七、方法表集合"></a>七、方法表集合</h3></li>
<li>对方法的描述和对字段的描述完全一致</li>
<li>方法表结构：<ul>
<li>访问标志(方法访问权限)</li>
<li>名称索引</li>
<li>描述符索引</li>
<li>属性表集合 (方法的实现)<h3 id="八、属性表集合"><a href="#八、属性表集合" class="headerlink" title="八、属性表集合"></a>八、属性表集合</h3></li>
</ul>
</li>
<li>概述：<ul>
<li>描述某些场景专有信息</li>
<li>class文件，字段表，方法表，都可以携带自己的方法表。</li>
</ul>
</li>
<li>1、code属性<ul>
<li>出现在方法表的属性集合中</li>
<li>接口和抽象类的抽象方法中就不存在code</li>
<li>this：<ul>
<li>实例方法的局部变量表中，至少存在一个指向当前实例的局部变量</li>
<li>局部变量表中也会预留出第一个slot位存放对象实例引用。</li>
</ul>
</li>
</ul>
</li>
<li>2、Exceptions属性<ul>
<li>列举方法中可能抛出的受检查的异常(throws 异常)</li>
</ul>
</li>
<li>3、LineNumberTable属性<ul>
<li>描述java源码和字节码行号之间的对应</li>
</ul>
</li>
<li>4、LocalVariableTable属性<ul>
<li>描述栈帧局部变量表中变量与java源码中定义变量的关系  </li>
</ul>
</li>
<li>5、contentValue属性<ul>
<li>通知虚拟机为static属性赋值</li>
<li>为非static变量属性赋值(obj成员变量)：<ul>
<li>实例构造器<init>方法中进行</init></li>
</ul>
</li>
<li>为static变量赋值(class变量)<ul>
<li>被final和static共同修饰的基本数据类型和String用ContentValue保存 </li>
</ul>
</li>
</ul>
</li>
<li>6、innerClass属性<ul>
<li>内部类和宿主类的关系</li>
</ul>
</li>
<li>7、Deprecated及Synthic属性<ul>
<li>Deprecated注解中的 @deprecated，表示该方法不推荐使用</li>
<li>Synthic不是java产生的，而是编译器自行产生的</li>
</ul>
</li>
<li>8、StackMapTable属性<ul>
<li>复杂的变长属性，jvm加载字节码验证阶段被新类型验证器使用</li>
</ul>
</li>
<li>9、Signature<ul>
<li>可选定长属性，出现于类、属性表、方发表的属性表中。</li>
<li>主要是针对java使用伪泛型，反射困难，而增设</li>
</ul>
</li>
<li>10、BootstrapMethod<ul>
<li>类文件的属性表中</li>
</ul>
</li>
</ul>


                <hr>

                

                <ul class="pager">
                    
                        <li class="previous">
                            <a href="/myblog/2016/11/01/http/确保web安全的HTTPS/" data-toggle="tooltip" data-placement="top" title="HTTPS介绍">&larr; Previous Post</a>
                        </li>
                    
                    
                        <li class="next">
                            <a href="/myblog/2016/10/01/jvm/jvm执行/class加载器/" data-toggle="tooltip" data-placement="top" title="类加载器">Next Post &rarr;</a>
                        </li>
                    
                </ul>

                

                
                <!-- disqus 评论框 start -->
                <div class="comment">
                    <div id="disqus_thread" class="disqus-thread"></div>
                </div>
                <!-- disqus 评论框 end -->
                

            </div>
    <!-- Side Catalog Container -->
        
            <div class="
                col-lg-2 col-lg-offset-0
                visible-lg-block
                sidebar-container
                catalog-container">
                <div class="side-catalog">
                    <hr class="hidden-sm hidden-xs">
                    <h5>
                        <a class="catalog-toggle" href="#">CATALOG</a>
                    </h5>
                    <ul class="catalog-body"></ul>
                </div>
            </div>
        

    <!-- Sidebar Container -->

            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                sidebar-container">

                <!-- Featured Tags -->
                
                <section>
                    <!-- no hr -->
                    <h5><a href="/myblog/tags/">FEATURED TAGS</a></h5>
                    <div class="tags">
                       
                          <a class="tag" href="/myblog/tags/#jvm" title="jvm">jvm</a>
                        
                          <a class="tag" href="/myblog/tags/#class" title="class">class</a>
                        
                    </div>
                </section>
                

                <!-- Friends Blog -->
                
                <hr>
                <h5>FRIENDS</h5>
                <ul class="list-inline">

                    
                        <li><a href="http://blog.kaijun.rocks" target="_blank">Kaijun&#39;s Blog</a></li>
                    
                        <li><a href="http://huangxuan.me" target="_blank">Hux Blog</a></li>
                    
                        <li><a href="#" target="_blank">Foo</a></li>
                    
                        <li><a href="#" target="_blank">Bar</a></li>
                    
                        <li><a href="#" target="_blank">Example Friends</a></li>
                    
                        <li><a href="#" target="_blank">It helps SEO</a></li>
                    
                </ul>
                
            </div>

        </div>
    </div>
</article>




<!-- disqus 公共JS代码 start (一个网页只需插入一次) -->
<script type="text/javascript">
    /* * * CONFIGURATION VARIABLES * * */
    var disqus_shortname = "hexo-theme-huxblog";
    var disqus_identifier = "http://reflectyi.gitee.io/myblog/2016/10/01/jvm/jvm执行/class文件结构/";
    var disqus_url = "http://reflectyi.gitee.io/myblog/2016/10/01/jvm/jvm执行/class文件结构/";

    (function() {
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
</script>
<!-- disqus 公共JS代码 end -->




<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>
<!-- anchor-js, Doc:http://bryanbraun.github.io/anchorjs/ -->
<script>
    async("https://cdn.bootcss.com/anchor-js/1.1.1/anchor.min.js",function(){
        anchors.options = {
          visible: 'always',
          placement: 'right',
          icon: '#'
        };
        anchors.add().remove('.intro-header h1').remove('.subheading').remove('.sidebar-container h5');
    })
</script>
<style>
    /* place left on bigger screen */
    @media all and (min-width: 800px) {
        .anchorjs-link{
            position: absolute;
            left: -0.75em;
            font-size: 1.1em;
            margin-top : -0.1em;
        }
    }
</style>



    <!-- Footer -->
    <!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                
                
                

                

                
                    <li>
                        <a target="_blank" href="https://www.facebook.com/stateInstance">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-facebook fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                

                
                    <li>
                        <a target="_blank"  href="https://github.com/stateInstance">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                

                

                </ul>
                <p class="copyright text-muted">
                    Copyright &copy; Hi,Ethan 2019 
                    <br>
                    Theme by <a href="http://huangxuan.me">Hux</a> 
                    <span style="display: inline-block; margin: 0 5px;">
                        <i class="fa fa-heart"></i>
                    </span> 
                    Ported by <a href="http://blog.kaijun.rocks">Kaijun</a> | 
                    <iframe
                        style="margin-left: 2px; margin-bottom:-5px;"
                        frameborder="0" scrolling="0" width="91px" height="20px"
                        src="https://ghbtns.com/github-btn.html?user=kaijun&repo=hexo-theme-huxblog&type=star&count=true" >
                    </iframe>
                </p>
            </div>
        </div>
    </div>
</footer>

<!-- jQuery -->
<script src="/myblog/js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="/myblog/js/bootstrap.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="/myblog/js/hux-blog.min.js"></script>


<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>

<!-- 
     Because of the native support for backtick-style fenced code blocks 
     right within the Markdown is landed in Github Pages, 
     From V1.6, There is no need for Highlight.js, 
     so Huxblog drops it officially.

     - https://github.com/blog/2100-github-pages-now-faster-and-simpler-with-jekyll-3-0  
     - https://help.github.com/articles/creating-and-highlighting-code-blocks/    
-->
<!--
    <script>
        async("http://cdn.bootcss.com/highlight.js/8.6/highlight.min.js", function(){
            hljs.initHighlightingOnLoad();
        })
    </script>
    <link href="http://cdn.bootcss.com/highlight.js/8.6/styles/github.min.css" rel="stylesheet">
-->


<!-- jquery.tagcloud.js -->
<script>
    // only load tagcloud.js in tag.html
    if($('#tag_cloud').length !== 0){
        async("http://reflectyi.gitee.io/myblog/myblog/js/jquery.tagcloud.js",function(){
            $.fn.tagcloud.defaults = {
                //size: {start: 1, end: 1, unit: 'em'},
                color: {start: '#bbbbee', end: '#0085a1'},
            };
            $('#tag_cloud a').tagcloud();
        })
    }
</script>

<!--fastClick.js -->
<script>
    async("https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js", function(){
        var $nav = document.querySelector("nav");
        if($nav) FastClick.attach($nav);
    })
</script>


<!-- Google Analytics -->


<script>
    // dynamic User by Hux
    var _gaId = 'UA-49627206-1';
    var _gaDomain = 'huangxuan.me';

    // Originial
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', _gaId, _gaDomain);
    ga('send', 'pageview');
</script>




<!-- Baidu Tongji -->

<script>
    // dynamic User by Hux
    var _baId = '4cc1f2d8f3067386cc5cdb626a202900';

    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<!-- Side Catalog -->

<script type="text/javascript">
    function generateCatalog (selector) {
        var P = $('div.post-container'),a,n,t,l,i,c;
        a = P.find('h1,h2,h3,h4,h5,h6');
        a.each(function () {
            n = $(this).prop('tagName').toLowerCase();
            i = "#"+$(this).prop('id');
            t = $(this).text();
            c = $('<a href="'+i+'" rel="nofollow">'+t+'</a>');
            l = $('<li class="'+n+'_nav"></li>').append(c);
            $(selector).append(l);
        });
        return true;    
    }

    generateCatalog(".catalog-body");

    // toggle side catalog
    $(".catalog-toggle").click((function(e){
        e.preventDefault();
        $('.side-catalog').toggleClass("fold")
    }))

    /*
     * Doc: https://github.com/davist11/jQuery-One-Page-Nav
     * Fork by Hux to support padding
     */
    async("/myblog/js/jquery.nav.js", function () {
        $('.catalog-body').onePageNav({
            currentClass: "active",
            changeHash: !1,
            easing: "swing",
            filter: "",
            scrollSpeed: 700,
            scrollOffset: 0,
            scrollThreshold: .2,
            begin: null,
            end: null,
            scrollChange: null,
            padding: 80
        });
    });
</script>





<!-- Image to hack wechat -->
<img src="http://reflectyi.gitee.io/myblog/myblog/img/icon_wechat.png" width="0" height="0" />
<!-- Migrate from head to bottom, no longer block render and still work -->

</body>

</html>
